<template>
  <div>
    <div class="head">
      <span
        @click="getLabel('最热')"
        :style="this.name == '最热' ? 'color: #007fff' : ''"
        >最热</span
      >
      <span
        @click="getLabel('最新')"
        :style="this.name == '最新' ? 'color: #007fff' : ''"
        >最新</span
      >
      <input type="text" placeholder="搜索标签" v-model="inpval" @keydown.enter="filData" />
    </div>
    <ul>
      <li
        class="item"
        v-for="(item, index) in $store.state.labelData"
        :key="index"
        :id="item.Id"
      >
        <div class="tag">
          <div class="info-box">
            <a>
              <div
                class="lazy thumb thumb loaded"
                style="background-size: contain;"
              >
                <img :src="item.tag.icon" alt="" />
              </div>
              <div class=" title">{{ item.tag.tag_name }}</div>
            </a>
            <div class="meta-box">
              <div class="meta subscribe">{{ item.tag.post_article_count }} 关注</div>
              <div class="meta article">{{ item.tag.concern_user_count  }} 文章</div>
            </div>
          </div>
          <div class="action-box">
            <button
              class="subscribe-btn"
              :class=" $store.state.userLabellData.find(ite=>ite.user==user&& ite.tag_id==item.tag_id)?'act':''"
              @click.prevent="subscribed(item)"
            >
            <span >
              {{
                $store.state.userLabellData.find(ite=>ite.user==user&& ite.tag_id==item.tag_id)?'已关注':'+ 关注'
              }}
            </span>
              
            </button>
          </div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      name: "最热",
      user: localStorage.getItem("isLogin"),
      inpval:''
    };
  },
  methods: {
    //点击最新最热
    getLabel(name) {
      console.log(name);
      this.name = name;
      this.$store.dispatch("getLabel", { name: this.name });
    },
    //点击关注
    
    subscribed(item){
      
      this.$store.dispatch("addLabel", { user: this.user ,item});
    },
    //搜索功能
    filData(){
      console.log(this.inpval)
      this.$store.commit('filData',this.inpval)
    }
  },
  created() {
    this.$store.dispatch("getLabel", { name: this.name });
  },
};
</script>

<style scoped lang="scss">
.head {
  width: 100%;
  margin: 25px;
  padding: 0 8px;
  span {
    margin-right: 25px;
    font-size: 14px;
  }
  input {
    width: 170px;
    height: 30px;
    line-height: 30px;
    border: 1px solid hsla(0, 0%, 59.2%, 0.2);
  }
}
ul {
  width: 100%;
  padding: 0 8px;
  li {
    width: 25%;
    display: inline-block;
    margin-bottom: 15.6px;
    padding: 0 8.4px;

    .tag {
      width: 100%;
      background-color: #fff;
      display: inline-block;
      border: 1px solid #f1f1f1;
      transition: border-color 0.3s;
      text-align: center;
      padding: 18px 0;

      .info-box {
        .thumb {
          img {
            height: 32px;
          }
        }
        .title{
          margin: 8px 0;
        }
      }

      .meta-box {
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 14px;
        color: #909090;

        .meta {
          line-height: 24px;
        }

        .meta.article {
          margin-left: 9.6px;
        }
      }

      .subscribe-btn {
        border: 1px solid #37c700;
        background-color: #fff;
        color: #37c700;
        margin: 12px auto;
        font-size: 14.4px;
      }
      .act{
        background-color: #37c700;
        color: #fff;
      }
      .subscribe-btn.subscribed {
        background-color: #37c700;
        color: #fff;
      }
    }
  }
}
</style>
